//
//  ByViewCitySearch.m
//  mijifen_ios
//
//  Created by 朱凯 on 2017/10/30.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByViewCitySearch.h"

@implementation ByViewCitySearch
{
    UITextField *_searchTf;
    UIButton    *_searchButton;
}
- (instancetype)init {
    if (self = [super init]) {
        self.backgroundColor = [UIColor whiteColor];
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    
    // 背景
    UIView *bgView = [UIView new];
    bgView.layer.cornerRadius = 20;
    bgView.layer.masksToBounds = true;
    bgView.backgroundColor = BY_BG_COLOR;
    [self addSubview:bgView];
    
    // 搜索
    UIImageView *searchIconView = [UIImageView new];
    searchIconView.image = [UIImage imageNamed:@"搜索"];
    [self addSubview:searchIconView];

    // 输入框
    _searchTf = [UITextField new];
    _searchTf.placeholder = @"请输入城市名，例如北京";
    _searchTf.font = FONT(16);
    _searchTf.textAlignment = NSTextAlignmentLeft;
    _searchTf.clearsOnBeginEditing = true;
    _searchTf.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self addSubview:_searchTf];
    
    // 按钮
    _searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _searchButton.titleLabel.font = FONT(16);
    [_searchButton setTitle:@"搜索" forState:UIControlStateNormal];
    [_searchButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_searchButton addTarget:self action:@selector(search) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_searchButton];
    
    
    
    // 布局
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(@10);
        make.bottom.equalTo(@-10);
        make.right.equalTo(_searchButton.mas_left);
    }];
    [searchIconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(20, 20));
        make.centerY.equalTo(@0);
        make.left.equalTo(bgView).offset(10);
    }];
    [_searchTf mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(searchIconView.mas_right).offset(10);
        make.top.bottom.equalTo(bgView);
        make.right.equalTo(bgView).offset(-10);
    }];
    [_searchButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(self);
        make.width.equalTo(@50);
        make.top.right.equalTo(@0);
    }];
}

- (void)search {
    
    if (self.searchBlock) {
        self.searchBlock(_searchTf.text);
    }
}

@end
